#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author:洪卫
 
import tkinter as tk  # 使用Tkinter前需要先导入
 
from tkinter import StringVar, IntVar

import time
import threading

import pysocket

isSendNow = True

def sendChange(p,ang):
    if isSendNow:
        moveobj = pysocket.getMoveObj()
        moveobj.setPwmMove(p, ang)
        moveobj.sendState()


class COMTool(object):
    """docstring for COMTool"""
    def __init__(self):
        #
        self.isConnect = False    #是否有机械臂连接

        self.datas = [90,90,90,90,90,90]

        self.window = tk.Tk()
        self.window.title('青苹果6轴机械臂')
        self.window.geometry('500x400')  # 这里的乘是小x

        self.scales = []
        scaletmp = tk.Scale(self.window,from_=0,to=180,tickinterval=15,orient=tk.HORIZONTAL,command=self.s0_print,length=400)
        scaletmp.place(x=25,y=20,anchor='nw')
        scaletmp.set(90)
        self.scales.append(scaletmp)


        scaletmp = tk.Scale(self.window,from_=0,to=180,tickinterval=15,orient=tk.HORIZONTAL,command=self.s1_print,length=400)
        scaletmp.place(x=25,y=80,anchor='nw')
        scaletmp.set(90)
        self.scales.append(scaletmp)

        scaletmp = tk.Scale(self.window,from_=0,to=180,tickinterval=15,orient=tk.HORIZONTAL,command=self.s2_print,length=400)
        scaletmp.place(x=25,y=140,anchor='nw')
        scaletmp.set(90)
        self.scales.append(scaletmp)

        scaletmp = tk.Scale(self.window,from_=0,to=180,tickinterval=15,orient=tk.HORIZONTAL,command=self.s3_print,length=400)
        scaletmp.place(x=25,y=200,anchor='nw')
        scaletmp.set(90)
        self.scales.append(scaletmp)

        scaletmp = tk.Scale(self.window,from_=0,to=180,tickinterval=15,orient=tk.HORIZONTAL,command=self.s4_print,length=400)
        scaletmp.place(x=25,y=260,anchor='nw')
        scaletmp.set(90)
        self.scales.append(scaletmp)

        scaletmp = tk.Scale(self.window,from_=0,to=180,tickinterval=15,orient=tk.HORIZONTAL,command=self.s5_print,length=400)
        scaletmp.place(x=25,y=320,anchor='nw')
        scaletmp.set(90)
        self.scales.append(scaletmp)


    def s0_print(self,txt):
        print(type(txt),txt)
        self.datas[0] = int(txt)
        sendChange(0, int(txt))

    def s1_print(self,txt):
        print(type(txt),txt)
        self.datas[1] = int(txt)
        sendChange(1, int(txt))


    def s2_print(self,txt):
        print(type(txt),txt)
        self.datas[2] = int(txt)
        sendChange(2, int(txt))


    def s3_print(self,txt):
        print(type(txt),txt)
        self.datas[3] = int(txt)
        sendChange(3, int(txt))


    def s4_print(self,txt):
        print(type(txt),txt)
        self.datas[4] = int(txt)
        sendChange(4, int(txt))


    def s5_print(self,txt):
        print(type(txt),txt)
        ang = int(txt)
        if ang > 90:
            ang = 90
        self.datas[5] = ang
        sendChange(5, ang)


    def uiLoop(self):
        # 第8步，主窗口循环显示
        self.window.mainloop()
    def showLog(self,pmsg):
        self.log.set(str(pmsg))
    
    def getData(self):
        out = []
        for i,v in enumerate(self.datas, 0):
            out.append(v)
        return out


def serialThread(pUtool):
    time.sleep(1)
    pysocket.start_serverThread()
    moveobj = pysocket.getMoveObj()
    count = 0
    while not moveobj.isHeaveClient():
        count += 1
        print('没有设备连接，等3秒后重试,\n第%d次重试\n如果尝试100次后还是没有设备连接则程序自动退出...'%(count))
        time.sleep(3)
        if count >= 100:
            return
    dats = pUtool.getData()
    pysocket.initAll(dats)
    # lastdat = dats
    # while True:
    #     time.sleep(0.01)
    #     try:
    #         dats = pUtool.getData()
    #         if lastdat != dats:
    #             lastdat = dats
    #             pysocket.initAll(dats)
    #     except Exception as e:
    #         print('数据错误')

def main():
    comtool = COMTool()
    serth = threading.Thread(target=serialThread,args=(comtool,))
    serth.setDaemon(True)
    serth.start()
    comtool.uiLoop()

if __name__ == '__main__':
    main()